<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['lang']){
	$_SESSION['show_lang']=$_RQ['lang'];
}
$return['mid']=$_SESSION['uid'];
$return['show_setmobile']=cfc('core')->check_setmobile(1);
$return['show_setmember']=cfc('core')->check_setmember(1);
if($_RQ['op']=='main'){
	//去除匿名下单功能
	if($_RQ['goodsid']){
		$login_burl='jy_liveshop/goods.detail//id='.$_RQ['goodsid'];
		$this->session('login_burl',$login_burl);
	}
	$return=model('jy_weishop/get_order')->getdata($_RQ);
	if($_SESSION['openid'.$merchid] || $_SESSION['onemerchid']){
		$return['onemerchid']=$_SESSION['onemerchid']?:$merchid;
	}
	foreach($return['order']['list'] as $item){
		$item['api_data']=json_decode($item['api_data'],true);
		if($item['api_data']['dingpei']){
			$return['post']['is_dingpei']=1;
			break;
		}
	}
	$return['sendtypes']=array(
		'1'=>'快递配送',
		'2'=>'上门自提',
	);
	if($return['sendtypes'][1]){
		$return['sendtypes'][1]=$this->send_types[1];
	}
	if($return['sendtypes'][2]){
		$return['sendtypes'][2]=$this->send_types[2];
	}
	if($this->plugin_exists('jy_area_dian')){
			$area_dian = pdo_get('jy_area_dian_agent',array('uniacid' =>$_SESSION['uniacid'],'status'=>1),'','id desc');
			if($area_dian){
				$area_dian['pcd']=json_decode($area_dian['pcd'],true);
				$return['area_dians'][]=$area_dian;
				$return['area_dian']=$area_dian;
				$return['post']['area_dianid']=$area_dian['id'];
			}
	}
	//返回链接
	$return['forward_url']=base64_encode('jy_liveshop/order.list//fromcreate=1');
	$return['pluginset']=$this->setting;
	set_html('title','确认订单');
	exi($return);
}
// 完成下单
if($_RQ['op']=='post'){
	$post=get_postdata();
	//整理订单数据
	$new_order=model('jy_weishop/get_order')->new_order($post);
	$new_order['plugin_name']='jy_liveshop';
	if(!$new_order['merchid'] && $_SESSION['onemerchid']){
		$new_order['api_data']=array('dhmerchid'=>$_SESSION['onemerchid']);
	}
	if($post['area_dianid']){
		$new_order['api_data']['area_dianid']=$post['area_dianid'];
	}
	$order_id=model('jy_weishop/order')->create($new_order);
	if($_SESSION['current_plugin'] && $_SESSION['current_plugin']!='core' && $_SESSION['current_plugin']!='jy_weishop'){
		$success_url=plugin($_SESSION['current_plugin'])->weishop_order_detail;
		if($success_url){
			$success_url=$_SESSION['current_plugin'].$success_url.'/id='.$order_id;
			$error_url=$success_url;
		}
	}
	$params=array(
		'id'=>$order_id,
		'title'=>'购买商品',
		'fee'=>$new_order['price'],
		'oldfee'=>$new_order['oldfee'],
		//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
		//'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
		'back_url'=>'jy_liveshop/order.create/payresult',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
		'success_url'=>$success_url?:'jy_liveshop/order.list/',
		'error_url'=>$error_url?:'jy_liveshop/order.list/',
	);
	//虚拟设置
	$plugin_xuni=model('jy_weishop/activity')->plugin_xuni($new_order);
	if(count($plugin_xuni)){
		$params['plugin_xuni']=$plugin_xuni;
	}
	$payid=cfc('pay')->create_pay_order($params);
	pdo_update('jy_weishop_order',array('pay_id'=>$payid),array('id'=>$order_id));
	if($post['peerpay']){
		$peerpay_setting=plugin('jy_weishop')->plugin_setting('peerpay');
		if($peerpay_setting && $peerpay_setting['open']){
			exi('','','jy_weishop/order.peerpay//orderid='.$order_id);
		}
	}
	$res=array(
		'pay_id'=>$payid,
		'order_id'=>$order_id,
		'success_url'=>$success_url,
		'error_url'=>$error_url,
	);
	exi($res);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_liveshop/order.list/');
	}
	$order=pdo_get('jy_weishop_order',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_liveshop/order.list/');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['price'].'|'.$payresult['fee'],'error','jy_liveshop/order.list/');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
	);
	model('jy_weishop/order')->order_pay($order['id'],$params);
	$message='有新的订单支付成功！';
	if($order['merchid']){
	}else{
		$this->model('commission')->get_order($order);
	}
	$order['api_data']=json_decode($order['api_data'],true);
	if($this->plugin_exists('jy_area_dian') && $order['api_data']['area_dianid']){
		model('jy_area_dian/daishou')->create_order($order,$order['api_data']['area_dianid']);
	}
	exi('提交成功!');
}

?>